package model

import business.Attackable
import business.Enhanceable
import business.Reinforceable
import com.sun.xml.internal.ws.api.server.LazyMOMProvider
import enums.Direction
import enums.EnhanceType
import org.itheima.kotlin.game.core.Composer
import org.itheima.kotlin.game.core.Painter

/**
 * 我方坦克
 * */
class MyTank(override var x: Int, override var y: Int) :Tank(x,y),Reinforceable {

    override var currentDirection: Direction=Direction.UP
    override val speed: Int=8
    override var pool: Int=1

    var list=ArrayList<Bullet>()

    var grade=1  //坦克等级

    override fun draw() {
        if(grade==1){
            imgPath=when(currentDirection){
                Direction.UP->"img/tank_u.gif"
                Direction.DOWN->"img/tank_d.gif"
                Direction.LEFT->"img/tank_l.gif"
                Direction.RIGHT->"img/tank_r.gif"
            }
        }else{
            imgPath=when(currentDirection){
                Direction.UP->"img/tank_2_u.gif"
                Direction.DOWN->"img/tank_2_d.gif"
                Direction.LEFT->"img/tank_2_l.gif"
                Direction.RIGHT->"img/tank_2_r.gif"
            }
        }

        super.draw()
    }

    override fun shot(gradle:Int): Bullet? {
        list.forEach {
            if(it.isDestory()){
                list.remove(it)
            }
        }
        //可以持有的子弹数
        var maxBulletCount=when(grade){
            1-> 1
            2-> 2
            else -> 1
        }

        if(list.size>=maxBulletCount){
            return null
        }

        /** 播放声音 */
        Composer.play("snd/hit.wav")

        var bullet=super.shot(this.grade)
        bullet?.let {
            list.add(bullet)
        }
        return bullet
    }

    override fun notifySuffer(attack: Attackable): ArrayList<IView> {
        var list=super.notifySuffer(attack)
        if(pool>=2){
            grade=2
        }else{
            grade=1
        }
        return list
    }

    override fun strengthen(enhance: Enhanceable): EnhanceType {
        Composer.play("snd/add.wav")
        if((enhance.type==EnhanceType.UPGRADE)and (grade<2)){
            grade++
        }
        return enhance.type
    }
}